package com.webdev.base.util;

import com.fasterxml.jackson.databind.ObjectMapper;
import com.webdev.base.pojo.User;

import java.io.*;
import java.net.HttpURLConnection;
import java.net.URL;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.Timestamp;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * @Package: com.seckillpro.seckill_pro.utils
 * @ClassName: UserUtils
 * @CreateTime: 2021/3/15 10:01
 * @Description: 生成用户工具类
 */
public class UserUtil {


    private static void createUser(int count) throws Exception {

        //循环创建user、
        List<User> users = new ArrayList<>(count);
        for (int i = 0; i < count; i++) {
            User user = new User();
            user.setUserId(2101210001 + i);
            user.setUserName("user" + i);
            user.setUserProvince("四川");
            user.setUserCity("攀枝花");
            user.setUserRegion("东区");
            user.setUserDetail("xxxx");
            user.setUserMobile("18012345678");
            users.add(user);
        }
        System.out.println("create user");

        //插入数据库
        Connection conn = getConn();
        String sql = "INSERT INTO user (user_id, user_name, user_province, user_city, user_region, user_detail, user_mobile) VALUES(?, ?, ?, ?, ?, ?, ?);";
        PreparedStatement pstmt = conn.prepareStatement(sql);
        for (int i = 0; i < users.size(); i++) {
            User user = users.get(i);
            pstmt.setInt(1, user.getUserId());
            pstmt.setString(2, user.getUserName());
            pstmt.setString(3, user.getUserProvince());
            pstmt.setString(4, user.getUserCity());
            pstmt.setString(5, user.getUserRegion());
            pstmt.setString(6, user.getUserDetail());
            pstmt.setString(7, user.getUserMobile());
            pstmt.addBatch();
        }
        pstmt.executeBatch();
        pstmt.clearParameters();
        conn.close();
        System.out.println("insert to db ");
        login(users);

    }

//    private static void selectUser() throws IOException {
//
//        TUserServiceImpl tUserService = new TUserServiceImpl();
//        List<TUser> users = tUserService.getUserAll();
//        login(users);
//    }

    private static void login(List<User> users) throws IOException {
        File file = new File("C:\\Users\\18359\\Desktop\\课程\\互联网软件开发\\config.txt");
        if (file.exists()) {
            file.delete();
        }
        RandomAccessFile raf = new RandomAccessFile(file, "rw");
        file.createNewFile();
        raf.seek(0);
        for (int i = 0; i < users.size(); i++) {
            User user = users.get(i);

            String row = user.getUserId() + ",1";
            raf.seek(raf.length());
            raf.write(row.getBytes());
            raf.write("\r\n".getBytes());
            System.out.println("write to file : " + row);
        }
        raf.close();

        System.out.println("over");
    }

    private static Connection getConn() throws Exception {
        String url = "jdbc:mysql://59.110.168.33:3307/giftseckill?" +
                "useUnicode=true&characterEncoding=UTF-8&serverTimezone=Asia/Shanghai";
        String driver = "com.mysql.cj.jdbc.Driver";
        String username = "root";
        String password = "123456";
        Class.forName(driver);
        return DriverManager.getConnection(url, username, password);
    }

    //生成5000个用户同时秒杀商品
    public static void main(String[] args) throws Exception {
        createUser(5000);
//        selectUser();
    }
}
